package mediatorPattern;

import java.util.ArrayList;
import java.util.List;

public class RentalAgency implements Mediator {

    private List<Landlord> landlords = new ArrayList<>();
    private List<Tenant> tenants = new ArrayList<>();

    // 添加房东
    public void addLandlord(Landlord landlord) {
        landlords.add(landlord);
    }

    // 添加租客
    public void addTenant(Tenant tenant) {
        tenants.add(tenant);
    }

    @Override
    public void notify(Object sender, String message) {
        if (message.equals("postHouseInfo")) {
            // 如果是房东发布房源，通知所有租客
            for (Tenant tenant : tenants) {
                tenant.receiveHouseInfo(((Landlord) sender).getHouseInfo());
            }
        } else if (message.equals("request_viewing")) {
            // 如果是租客请求看房，通知对应的房东
            for (Landlord landlord : landlords) {
                if (landlord.getHouseInfo().equals(((Tenant) sender).getRequestedHouseInfo())) {
                    landlord.receiveViewRequest((Tenant) sender);
                }
            }
        }
    }
}
